/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.gwt.basics.bean.impl;

import java.io.InputStream;

import com.genia.toolbox.basics.bean.DataContainer;
import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.web.gwt.basics.bean.FileInformation;
import com.genia.toolbox.web.gwt.basics.client.bean.GwtFileInformation;

/**
 * Base implementation of {@link FileInformation}.
 */
public abstract class AbstractFileInformation
    implements FileInformation
{

  /**
   * the {@link GwtFileInformation} to send to the client.
   */
  private GwtFileInformation gwtFileInformation;

  /**
   * the identifier of this uploaded file on the server, or <code>null</code>
   * if this file is newly uploaded.
   */
  private Long permanentIdentifier;

  /**
   * the DataContainer backing this {@link FileInformation}.
   */
  private DataContainer dataContainer;



  /**
   * getter for the gwtFileInformation property.
   * 
   * @return the gwtFileInformation
   * @see com.genia.toolbox.web.gwt.basics.bean.FileInformation#getGwtFileInformation()
   */
  public GwtFileInformation getGwtFileInformation()
  {
    return gwtFileInformation;
  }



  /**
   * getter for the permanentIdentifier property.
   * 
   * @return the permanentIdentifier
   */
  public Long getPermanentIdentifier()
  {
    return permanentIdentifier;
  }



  /**
   * setter for the permanentIdentifier property.
   * 
   * @param permanentIdentifier
   *          the permanentIdentifier to set
   */
  public void setPermanentIdentifier(Long permanentIdentifier)
  {
    this.permanentIdentifier = permanentIdentifier;
  }



  /**
   * returns the {@link InputStream} to the uploaded file.
   * 
   * @return the {@link InputStream} to the uploaded file *
   * @throws TechnicalException
   *           when an error occured
   * @see com.genia.toolbox.web.gwt.basics.bean.FileInformation#getInputStream()
   */
  public InputStream getInputStream()
      throws TechnicalException
  {
    return dataContainer.getInputStream();
  }



  /**
   * returns the type of the file.
   * 
   * @return the type of the file
   * @see com.genia.toolbox.basics.bean.DataContainer#getContentType()
   */
  public String getContentType()
  {
    return getGwtFileInformation().getContentType();
  }



  /**
   * returns the name of the file.
   * 
   * @return the name of the file
   * @see com.genia.toolbox.basics.bean.DataContainer#getName()
   */
  public String getName()
  {
    return getGwtFileInformation().getName();
  }



  /**
   * getter for the dataContainer property.
   * 
   * @return the dataContainer
   */
  public DataContainer getDataContainer()
  {
    return dataContainer;
  }



  /**
   * setter for the dataContainer property.
   * 
   * @param dataContainer
   *          the dataContainer to set
   */
  public void setDataContainer(DataContainer dataContainer)
  {
    this.dataContainer = dataContainer;
    gwtFileInformation = createGwtFileInformation();
    gwtFileInformation.setContentType(getDataContainer().getContentType());
    gwtFileInformation.setName(getDataContainer().getName());
  }



  /**
   * factory method for {@link GwtFileInformation}.
   * 
   * @return a new {@link GwtFileInformation}
   */
  public abstract GwtFileInformation createGwtFileInformation();
}
